//
//  InfoDetail.m
//  Trading365
//
//  Created by Jason on 12-2-3.
//  Copyright (c) 2012年 dhc. All rights reserved.
//

#import "QuotationInfo3.h"
#import "InfoType.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "QuotCostModelListCell.h"


@implementation QuotationInfo3
@synthesize table;
@synthesize InfoListTitle,InfoListName;
@synthesize responseParams;
@synthesize quotationNO3;


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title=self.key_name;
    self.table.backgroundColor=BACKGROUND_COLOR;

    NSArray *title_array = [[NSArray alloc] initWithObjects:@"",nil];
    self.InfoListTitle = title_array; 
    [title_array release];
   
    NSArray *name_title = [[NSArray alloc] initWithObjects:@"",nil];
    self.InfoListName = name_title; 
    [name_title release];
    
    self.table.frame = CGRectMake(0, 0, 320, 480-49-44-20);
    [self.table reloadData];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    [[[TaskManager Share] GetTaskByType:GetQuotationInfo3 Delegate:self] cancel];
}

- (void)dealloc {
    [responseParams release];
    [InfoListTitle release];
    [InfoListName release];
    [quotationNO3 release];
    [table release];
    [super dealloc];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.responseParams count];    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *CellIdentifier = @"QuotCostModelListCell";
    
    QuotCostModelListCell* cell = (QuotCostModelListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"QuotCostModelListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    NSUInteger row = [indexPath row];
    
    if ([responseParams count] < 1)
        return cell;

    if (row>[responseParams count]) {
        return cell;
    }

    NSMutableDictionary* dic = nil;
    if (indexPath.row < [responseParams count]) 
    {
        dic = [[NSMutableDictionary alloc] initWithDictionary:[responseParams objectAtIndex:row]]; 
    }else
    {
        dic = [[NSMutableDictionary alloc] initWithCapacity:0];
    }
  
    cell.name.text =[dic objectForKey:@"dict_name_cn"];
    
    float val=[[dic objectForKey:@"profit_loss_item_rate"] floatValue];
    NSString *tmp_val=[NSString stringWithFormat:@"%.2f",val];
    cell.value1.text =tmp_val;//比率
    
    val=[[dic objectForKey:@"profit_loss_item_value_rmb"] floatValue];
    tmp_val=[NSString stringWithFormat:@"%.2f",val];
    cell.value2.text =tmp_val;//人民币值
    
    val=[[dic objectForKey:@"profit_loss_item_value_usd"] floatValue];
    tmp_val=[NSString stringWithFormat:@"%.2f",val];
    cell.value3.text =tmp_val;//美元值

    val=[[dic objectForKey:@"profit_loss_item_value"] floatValue];
    tmp_val=[NSString stringWithFormat:@"%.2f",val];
    cell.value4.text =tmp_val; //原币值
    
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [dic release];
                
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.  
    
//    ProductInfoDetail *info = [[ProductInfoDetail alloc] init];
//    [info setInfoDetail:responseParam];
//    [self.navigationController pushViewController:info animated:YES];
//    [info release];
     
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

#pragma mark - 
#pragma mark - my func

-(void) OnPreExcute
{
    [super OnPreExcute];
}

-(void) DoInBackground
{
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableArray *tmp;

    tmp = [xml GetCostModel:self.quotationNO3];
    
    responseParams = [[NSMutableArray alloc] initWithArray:tmp];

    [xml release];
    [self.table reloadData];
}

-(void) OnPostExcute
{
    [super OnPostExcute];
}

-(void)refreshData
{    
    [[[TaskManager Share] GetTaskByType:GetQuotationInfo3 Delegate:self] Excute];
}

@end
